<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Routes
 *
 * @author Loug S
 */
class Routes {
    //put your code here
    
 
    /**
    * Construit un tableau de paramètres à partir d'une URL
    * @return
    */
    public static function recognizeURL($url) {
         $routes = require './config/routes.php';
         $tabUrl = explode('/', $url);
             foreach ($routes as $route) {
                $routeUrl = substr($route['url'], 1, strlen($route['url'])-1);
                // 1er test, le nombre de params
                $tabRouteUrl = explode('/', $routeUrl);
                if (count($tabRouteUrl) != count($tabUrl)) {
                         continue;
                }
                // ensuite, test si la regexp de l'url est bonne
                preg_match_all("/:([a-zA-Z0-9]*)/", $routeUrl, $neededParams);
                $url_regexp = $routeUrl;
                    foreach ($neededParams['1'] as $p) {
                        // On cherche si une regexp est definit dans la route
                        $regexp = (isset($route['requirements'][$p]) ? $route['requirements'][$p] : "\W");
                        $regexp = '(?P<'.$p.'>'.$regexp.')';
                        $url_regexp = preg_replace("/(:".$p.")/", $regexp, $url_regexp);
                    }
            $url_regexp = '#^'.$url_regexp.'$#i';
            if (preg_match($url_regexp, $url, $matches)) {
                 foreach($matches as $key => $match) {
                   if (is_int($key)) {
                     unset($matches[$key]);
                    }
                 }
                    if (! isset($route['params'])) {
                        $route['params'] = array();
                    }
                return array_merge($matches, $route['params']);
            }
         }
 
         }
}

?>
